package com.lzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.dao.PictrueDao;
import com.lzx.entity.Pictrue;
import com.lzx.service.BrandService;
import com.lzx.service.PictrueService;
import com.lzx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * (Pictrue)表服务实现类
 *
 * @author makejava
 * @since 2021-03-01 15:14:02
 */
@Service("pictrueService")
public class PictrueServiceImpl extends ServiceImpl<PictrueDao, Pictrue> implements PictrueService {
    @Value("${upload.path}")
    private String filePath;
    @Value("${upload.staticPath}")
    private String vtPath;

    private final String PRODUCT ="product";
    private final String BRAND ="brand";

    @Resource
    private PictrueDao pictrueDao;

    @Autowired
    private ProductService productService;
    @Autowired
    private BrandService brandService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Pictrue queryById(Integer id) {
        return this.pictrueDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Pictrue> queryAllByLimit(int offset, int limit) {
        return this.pictrueDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param pictrue 实例对象
     * @return 实例对象
     */
    @Override
    public Pictrue insert(Pictrue pictrue) {
        this.pictrueDao.insert(pictrue);
        return pictrue;
    }

    /**
     * 修改数据
     *
     * @param pictrue 实例对象
     * @return 实例对象
     */
    @Override
    public Pictrue update(Pictrue pictrue) {
        this.pictrueDao.update(pictrue);
        return this.queryById(pictrue.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.pictrueDao.deleteById(id) > 0;
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    @Override
    public String uploadPic(MultipartFile file, String picType) {
        String filename = UUID.randomUUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        File newfile = new File(filePath + File.separator + picType + File.separator + filename);
        if(!newfile.getParentFile().exists()){
            newfile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(newfile);
            String url = picType+File.separator+filename;
            Pictrue pictrue = new Pictrue();
            return url;
        } catch (IOException e) {
            return "上传失败";
        }
    }

    /**
     * 获取默认展示图片
     *
     * @return
     */
    @Override
    public Pictrue getDefaultImg(String type,Integer typeId) {
        Pictrue defaultImg = pictrueDao.queryDefaultImg(type, typeId);

        if(BeanUtil.isNotEmpty(defaultImg)){
            return defaultImg;
        }
        return new Pictrue();
    }

    /**
     * 获取当前商品的所有展示图
     *
     * @param type
     * @param typeId
     * @return
     */
    @Override
    public List<Pictrue> getAllByTypeAndTypeId(String type, Integer typeId) {

        return pictrueDao.queryAllByTypeAndTypeId(type, typeId);
    }

    /**
     * 判断图片所属成员是否存在
     * @param type
     * @param typeId
     * @return
     */
    @Override
    public Boolean isItemHave(String type, Integer typeId){
        Object item = new Object();
        if(PRODUCT.equals(type)){
            item = productService.getById(typeId);
        }
        if(BRAND.equals(type)){
            item = brandService.getById(typeId);
        }
        return BeanUtil.isNotEmpty(item);
    }
}
